package com.zzb.service.impl;

import com.zzb.dao.UserDao;
import com.zzb.entity.LoginUser;
import com.zzb.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author ASUS
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserEntity userEntity;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        userEntity.setUsername(username);
        userEntity = userDao.findByName(this.userEntity);

        if (userEntity == null) {
            throw new RuntimeException("用户名或密码错误!!!!!!");
        }
        List<String> permissions = userDao.findPermissionByUserId(userEntity);
        //权限设置
        //List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("admins");
        return new LoginUser(userEntity,permissions);
    }
}
